
%derivate
dExpr(T,DT)        :-dTerm(T,DT).
dExpr(E+T,DE+DT) :-dExpr(E,DE),dTerm(T,DT).
dExpr(E+T,DE-DT) :-dExpr(E,DE),dTerm(T,DT).

dTerm(F,DF)                      :-dFactor(F,DF).
dTerm(T*F,DT*F+T*DF)       :-dTerm(T,DT),dFactor(F,DF).
dTerm(T/F,DT*F-T*DF/F*F) :-dTerm(T,DT),dFactor(F,DF).

dFactor(x,1).
dFactor(x^K,K*x^N)	     :-number(K),N is K-1.
dFactor(N,0)                 :-number(N).
dFactor([E],DE)              :-dExpr(E,DE).
dFactor(-E,-DE)              :-dExpr(E,DE).
dFactor(sin(E),DE*cos(E))  :-dExpr(E,DE).
dFactor(cos(E),-DE*sin(E)) :-dExpr(E,DE).
dFactor(log(E),DE/E)	     :-dExpr(E,DE).
dFactor(exp(E),exp(E)*DE)    :-dExpr(E,DE).


